package popoer.price.thief.service;

import java.util.logging.Level;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import popoer.price.thief.domain.Category;
import popoer.price.thief.domain.Product;

public class AmazonThief extends BaseThief implements Thief {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AmazonThief.class.getName());

	private String baseURL = "http://www.amazon.cn/";
	private String encoding = "utf-8";
	
	@Override
	public List<Category> getCategories() throws Exception {
		List<Category> categories = new ArrayList<Category>();
		Category root = new Category();
		root.setName("所有分类");
		categories.add(root);
		String html = getContent("");
		logger.fine("String html=" + html); //$NON-NLS-1$
		Pattern regex = Pattern.compile("<option.*?value=\"(.*?)\".*?>(.*?)</option>", Pattern.DOTALL);
		Matcher regexMatcher = regex.matcher(html);
		while (regexMatcher.find()) {
			String value = regexMatcher.group(1);
			String text = regexMatcher.group(2);
			String url = "s/ref=nb_sb_noss?__mk_zh_CN=%D1%C7%C2%ED%D1%B7%CD%F8%D5%BE&url="+URLEncoder.encode(value,getEncoding())+"&field-keywords=";
			if(text.trim().equals("所有类别")){
				continue;
			}
			Category category = new Category();
			category.setLink(url);
			category.setName(text);
			root.addChild(category);
		}		
		return categories;
	}

	@Override
	public List<Product> getProducts(String relativeURL) throws Exception {
		List<Product> products = new ArrayList<Product>();		
		
		int round = 1;
		Map<String, String> cookies = new HashMap<String, String>();
		String referer = null;
		while(products.size()<200 && StringUtils.isNotEmpty(relativeURL)){
			String html = getContent(relativeURL,cookies,referer);
			if (logger.isLoggable(Level.FINE)) {
				logger.fine("String - String html=" + html); //$NON-NLS-1$
			}
			Pattern regex = Pattern.compile("title\" href=\"(.*?)\">(.*?)<.*?￥(.*?)<.*?￥(.*?)<", Pattern.DOTALL);
			Matcher matcher = regex.matcher(html);
			while(matcher.find()){
				Product product = new Product(matcher.group(2), NumberUtils.toFloat(matcher.group(3).trim()), matcher.group(1));
				product.setPriceImg(matcher.group(4).trim());
				products.add(product);
			}
			
			referer = getBaseURL() + relativeURL;		
			regex = Pattern.compile("pagnNext\" href=\"/(.*?)\">");
			matcher = regex.matcher(html);
			if(matcher.find()){
				relativeURL = matcher.group(1).replaceAll("&amp;", "&").replaceAll("http:\\/\\/www\\.amazon\\.cn\\/", "");
			}else{
				relativeURL = null;
			}
			logger.info("relativeURL:"+relativeURL);
			logger.info("round:"+round++);
		}
		return products;
	}

	@Override
	public String getBaseURL() {
		return this.baseURL;
	}

	@Override
	public String getEncoding() {
		return this.encoding;
	}

}
